package com.sky.mapper;

import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SetmealMapper {
    /**
     * 根据条件分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    List<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);
    /**
     * 保存套餐的基本信息
     * @param setmeal
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into setmeal (category_id, name, price, status, description, image, create_time, update_time, create_user, update_user) " +
            "values (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})" )
    void save(Setmeal setmeal);

    /**
     * 根据id查询套餐
     * @param idLong
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{id}")
    Integer contById(Long idLong);

    /**
     * 修改套餐
     * @param setmeal
     */
    @AutoFill(OperationType.UPDATE)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void update(Setmeal setmeal);

    /**
     * 修改套餐分类id
     * @param id
     * @param categoryId
     */
    @AutoFill(OperationType.UPDATE)
    @Options
    @Update("update setmeal set category_id = #{categoryId} where id = #{id}")
    void updateCategoryId(Long id, Long categoryId);

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Select("select * from setmeal where id = #{id}")
    SetmealVO getById(Long id);

    /**
     * 根据id查询套餐分类名称
     * @param id
     * @return
     */
    @Select("select C.name from setmeal S LEFT JOIN category C ON S.category_id = C.id")
    String getCategoryName(Long id);

    /**
     * 根据id删除套餐
     * @param id
     */
    @Delete("delete from setmeal where id = #{id}")
    void deleteById(Long id);

    /**
     * 批量删除套餐
     * @param status
     * @param id
     */
    @AutoFill(OperationType.UPDATE)
    @Update("update setmeal set status = #{status} where id in (${id})")
    void startOrStop(Integer status, Long id);

    /**
     * 根据条件查询套餐
     * @param setmeal
     * @return
     */
    /**
     * 动态条件查询套餐
     * @param setmeal
     * @return
     */
    List<Setmeal> list(Setmeal setmeal);

    /**
     * 根据套餐id查询菜品选项
     * @param setmealId
     * @return
     */
    @Select("select sd.name, sd.copies, d.image, d.description " +
            "from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
            "where sd.setmeal_id = #{setmealId}")
    List<DishItemVO> getDishItemBySetmealId(Long setmealId);
}
